package tools;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import models.HttpResponse;

import java.util.Collections;
import java.util.Objects;

/**
 * Created by yangxianming on 2019/4/1.
 * phone  1344444444,13666666666 判断是否是多个手机号
 * where=url1/url2 2个不同的钉钉提醒群
 */
public class DDAlert {
    public void send(String type, String where, String[] message, String phones){
        doSend(type, where, message, phones);
    }

    public void send(String where, String message, String phones){
        doSend("text", where, new String[]{message}, phones);
    }

    public void send(String message, String phones){
        doSend("text", "url3", new String[]{message}, phones);
    }

    private void doSend(String type, String where, String[] message, String phones){
        Http http = new Http();
        JSONObject header = new JSONObject();
        header.put("Content-Type", "application/json");
        String url = "";
        //小明1号
        if (Objects.equals(where, "url1")){
            url = "https://oapi.dingtalk.com/robot/send?access_token=9f7df18d06f4b7b11ada3e5e05e3a38ff7173bc78cf91ea17c25f141ba5e171c";
        }
        //风控报警
        if (Objects.equals(where, "url2")){
            url = "https://oapi.dingtalk.com/robot/send?access_token=b410feab0a80c83c8384851f9ef8e242efb6232dfe1a2ab3ea6517a9d08d52df";
        }
        //自动化测试报警群
        if (Objects.equals(where, "url3")){
            url = "https://oapi.dingtalk.com/robot/send?access_token=bc9791ba2f884d34842c71148bff1fdc855e647f20f0cdbfc9454c0015ee1f8b";
        }

        //组装联系人
        JSONArray atMobiles = new JSONArray();
        if (phones.contains(",")){
            String[] arrPhone = phones.split(",");
            Collections.addAll(atMobiles, arrPhone);
        }else {
            atMobiles.add(phones);
        }
        JSONObject at = new JSONObject();
        at.put("atMobiles", atMobiles);
        at.put("isAtAll", false);

        //取分消息类型
        JSONObject alertMessage = new JSONObject();
        alertMessage.put("msgtype", type);
        if (Objects.equals(type, "text")){
            JSONObject content = new JSONObject();
            content.put("content", message[0]);
            alertMessage.put("text", content);
        }
        if (Objects.equals(type, "markdown")){
            String threadNumber = message[0];
            JSONObject markDown = new JSONObject();
            String picUrl = "https://efulidev.oss-cn-hangzhou.aliyuncs.com/" + message[1];
            markDown.put("title", threadNumber + "被踢下线了请手动登陆下");
            markDown.put("text", threadNumber + "被踢下线了请手动登陆下 \n![alt 啊](" + picUrl + ")");
            alertMessage.put("markdown", markDown);
        }
        if (Objects.equals(type, "link")){
            JSONObject content = new JSONObject();
            content.put("text", message[0]);
            content.put("title", message[1]);
            content.put("picUrl", message[2]);
            content.put("messageUrl", message[3]);
            alertMessage.put("link", content);
        }

        //添加 at 人
        alertMessage.put("at", at);

        System.out.println(alertMessage.toJSONString());
        try {
            HttpResponse r = http.sendRequest("post", header, url, alertMessage.toJSONString());
            System.out.println(r.getResponseResult());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
